<?php
namespace App\Services;
use App\Models\Notification;
use Illuminate\Support\Facades\Redis;

class NotifyService {

    /**
     * store
     * @param $user_id
     * @param $type
     * @param $entity_id
     * @param $entity_user_id
     * @param null $content
     * @param int $comment_id
     * @param  $title
     */
    public static function store($user_id, $type, $entity_id, $entity_user_id, $content = null, $comment_id = 0, $title = '') {
        if ($user_id == $entity_user_id)
            return;

//        $notify = Notification::where('user_id', $user_id)->where('entity_user_id', $entity_user_id)->first();
        //关注
//        if ($notify) {
//            if ($notify->type == 2 || $notify->type == 1) {
//                $notify->delete();
//            }
//        }
        if ($entity_user_id) {
            if ($type == Notification::TYPE_FOCUS) {
                Redis::incr('user_latest_fans_count_' . $entity_user_id, 1);
            } else if ($type == Notification::TYPE_COMMENT_STAR || $type == Notification::TYPE_ARTICLE_STAR) {
                Redis::incr('user_latest_star_count_' . $entity_user_id, 1);
            } else if($type == Notification::TYPE_POST_COMMENT || $type == Notification::TYPE_POST_REPLY) {
                Redis::incr('user_latest_comment_count_' . $entity_user_id, 1);
            }
        }

        return Notification::create([
            'user_id' => $user_id,
            'type' => $type,
            'entity_id' => $entity_id,
            'entity_user_id' => $entity_user_id,
            'title' => $title,
            'content' => $content,
            'comment_id' => $comment_id
        ]);
    }

}
